package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.calculator.unit.constant.ConvertType;
import com.stark.calculator.unit.model.UnitBean;
import com.stark.calculator.unit.model.UnitDataProvider;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MyUnitAdapter;
import flc.ast.bean.MyUnitBean;
import flc.ast.databinding.ActivitySelLengthBinding;
import stark.common.basic.event.EventStatProxy;

public class SelLengthActivity extends BaseAc<ActivitySelLengthBinding> {

    private List<MyUnitBean> myUnitBeans;
    private List<UnitBean> mAllUnitBean;
    private String selName = "";
    private MyUnitAdapter myUnitAdapter;
    private int oldPosition;
    public static ConvertType convertType = ConvertType.LENGTH;


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_length;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        if (convertType == ConvertType.LENGTH) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_length_title));
        } else if (convertType == ConvertType.AREA) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_area_title));
        } else if (convertType == ConvertType.VOLUME) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_volume_title));
        }else if (convertType == ConvertType.WEIGHT) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_weight_title));
        }else if (convertType == ConvertType.TEMPERATURE) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_temperature_title));
        }else if (convertType == ConvertType.SPEED) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_speed_title));
        }else if (convertType == ConvertType.POWER) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_power_title));
        }else if (convertType == ConvertType.CALORIES) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_calories_title));
        }else if (convertType == ConvertType.PRESSURE) {
            mDataBinding.tvSelLengthTitle.setText(getString(R.string.sel_pressure_title));
        }
        mDataBinding.ivSelLengthBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        myUnitAdapter = new MyUnitAdapter();
        mDataBinding.rvLengthList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mDataBinding.rvLengthList.setAdapter(myUnitAdapter);
        myUnitAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        myUnitAdapter.getItem(oldPosition).setSelect(false);
        myUnitAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        myUnitAdapter.getItem(position).setSelect(true);
        myUnitAdapter.notifyItemChanged(position);
        selName = myUnitAdapter.getItem(position).getCnName();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        selName = intent.getStringExtra("selName");
        myUnitBeans = new ArrayList<>();
        mAllUnitBean = UnitDataProvider.getDatas(convertType);
        for (int i = 0; i < mAllUnitBean.size(); i++) {
            if (mAllUnitBean.get(i).cnName.equals(selName)) {
                myUnitBeans.add(new MyUnitBean(mAllUnitBean.get(i).cnName, mAllUnitBean.get(i).enName, mAllUnitBean.get(i).value, true));
                oldPosition = i;
            } else {
                myUnitBeans.add(new MyUnitBean(mAllUnitBean.get(i).cnName, mAllUnitBean.get(i).enName, mAllUnitBean.get(i).value, false));
            }
        }
        myUnitAdapter.setList(myUnitBeans);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("selName", selName);
        setResult(RESULT_OK, intent);
        finish();
    }
}